﻿@model Jb.Web.ViewModels.UtilityModels.ListConfig
@using Jb.Web.Utility;

@{
    bool _IsPublishedJobs = String.Compare(WebUtil.GetQueryStringValue("Type"), "P", true) == 0 ? true : false;//Jb.Common.DB.NullHandler.GetBoolean(ViewData[AppConstants.ViewDataKeys.GENERIC_KEY]);
    
    String pageTitle = _IsPublishedJobs ? "Published Job List" : "Job List";
    ViewBag.Title = Html.GetPageTitle(pageTitle);
    ViewData[AppConstants.ViewDataKeys.SELECTED_MENU] = EmployerMenu.ManageJobs; //_IsPublishedJobs ? EmployerMenu.PublishedJobs : EmployerMenu.ManageJobs;
    ViewData[AppConstants.ViewDataKeys.SHOW_EDIT_LINK] = true;
    bool _ShowForEmployer = true;
}

<div class="jb-page-heading">
    <h1>@pageTitle</h1>
    <div>
        @if(_IsPublishedJobs){
            <text>List of Published jobs only. Most recent published jobs comes at first.</text>
        }
        else{
            <text>Add, Edit and manage your jobs. You can view Job Applications also from here.</text>
        }
    </div>
</div>
@*@{String autoClass = Model.Pager != null && Model.Pager.TotalRecord > 0 ? "auto" : String.Empty;}*@
<div class="jb-page-content auto">
    @if (!PackageInfo.CanPostJob)
    {
        @Html.WarningMessageBox("You need to subscribe to a package to post jobs. <a href='/Package/List'>Click here</a> to subscribe to a package.");
    }
    <div class="pull-left">
    @if (PackageInfo.CanPostJob) { <a href="/Private/Job/Edit">Add New Job</a> }
    </div>
    <div class="btn-group pull-right">
        <a class="btn btn-mini@(_IsPublishedJobs ? String.Empty : " btn-primary")" href="/Private/Job/List"><i class="icon-list-alt@(_IsPublishedJobs ? String.Empty : " icon-white")"></i> All (@SessionCache.CurrentUser.EmployerInfo.TotalJobs)</a>
        <a class="btn btn-mini@(_IsPublishedJobs ? " btn-primary" : String.Empty)" href="/Private/Job/List?Type=P"><i class="icon-ok@(_IsPublishedJobs ? " icon-white" : String.Empty)"></i> Published Only (@SessionCache.CurrentUser.EmployerInfo.TotalActiveJobs)</a>
    </div>
    <div class="clear"></div>
    <div id="divMessage"></div>
</div>

@Html.Partial("_JobList", Model.DataSource)
@if (Model.Pager != null)
{
    String autoClass = Model.Pager.TotalRecord > 0 ? "auto" : String.Empty;
    <div class="jb-page-content @(autoClass)">
    @Html.Partial("_Pager", Model.Pager)
    </div>
}

@*Delete Confirmation Modal Popup Section Start*@
<div id="divConfirmDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="divPhotoEditModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">X</button>
        <h4 id="myModalLabel">Confirm Delete</h4>
    </div>
    <div class="modal-body jb-inline-label">            
        <div id="divModalMesssage">Are sure you want to delete this job?</div>
    </div>
    <div class="modal-footer">               
        <button type="button" class="btn btn-primary" onclick="DeleteJob();"><i class="icon-remove icon-white"></i> Delete</button>
        <button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i> Cancel</button>
    </div>
</div>
@*Delete Confirmation Modal Popup Section End*@
@if (!_ShowForEmployer)
{
@Html.Partial("_SendToFriendModal")
}

@section JavaScriptPlaceHolder{
    @Html.JavaScript("jquery.validate.min.js", Url)
    @Html.JavaScript("jquery.validate.unobtrusive.min.js", Url)
    @if (!_ShowForEmployer)
    {
    @Html.Partial("_SendToFriendJS")
    }
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $('.delete-job').each(function () {
                $(this).click(function () {
                    ConfirmDelete($(this), $(this).attr('delete-job-id'));
                });
            });
        });
        var _DeleteAnchor = null;
        function ConfirmDelete(element, resumeID) {
            _DeleteAnchor = element;
            $('#divConfirmDialog').modal({ show: true });
        }
        function DeleteJob() {
            $('#divConfirmDialog').modal('hide');
            ShowLoadingAnimation(false);
            Ajax('@Url.PrivateUrl("DeleteJob", "Job")', 'jobID=' + $(_DeleteAnchor).attr('delete-job-id')
                , function (result) {
                    HideLoadingAnimation();
                    var status = (parseInt(result));
                    if (status > 0) {
                        $(_DeleteAnchor).parent().parent().slideUp('slow');
                        DecreaseJobCount(status);
                    }
                    $('#divMessage').html(BuildDeleteMessage(status));
                });
        }

        function ForceDelete(jobID) {
            ShowLoadingAnimation(false);
            Ajax('@Url.PrivateUrl("DeleteJob", "Job")', 'jobID=' + jobID + '&forceDelete=True', function (result) {
                HideLoadingAnimation();
                $('#divMessage').html(BuildDeleteMessage(parseInt(result)));
                $(_DeleteAnchor).parent().parent().slideUp('slow');
                DecreaseJobCount(parseInt(result));
            });
        }
        function BuildDeleteMessage(result) {
            var html = result > 0 ? '<div class="alert alert-success">' : '<div class="alert alert-block">';
            html += '<button type="button" class="close" data-dismiss="alert">&times;</button>';
            if (result > 0)
                html += '<h4>Success!</h4> Job Deleted Successfully';
            else if (result == -1)
                html += '<h4>Warning!</h4> Could not find the job to delete';
            else if (result == -2)
                html += '<h4>Warning!</h4> This job contains job applications, Are you sure you want to delete this? <a href="#" onclick="ForceDelete(' + $(_DeleteAnchor).attr('delete-job-id') + ')" class="btn btn-mini">Force Delete Job</a>';
            else if (result == -3)
                html += '<h4>Warning!</h4> This job contains favorite resume(s), Are you sure you want to delete this? <a href="#" onclick="ForceDelete(' + $(_DeleteAnchor).attr('delete-job-id') + ')" class="btn btn-mini">Force Delete Job</a>';
            else
                html += '<h4>Warning!</h4> You are not allowed to delete this job.';
            html += '</div>';

            return html;
        }
        function DecreaseJobCount(status) {
            if(status == 1 || status == 2)
                $('#span_LM_Total_P_Jobs').html(parseInt($('#span_LM_Total_P_Jobs').html()) - 1);
             
            $('#span_LM_Total_Job').html(parseInt($('#span_LM_Total_Job').html()) - 1);
            //$('.PagerTotalRecordMessage').html('');
        }        
    </script>
}

